<?php
/**
 * Created by PhpStorm.
 * User: John
 * Date: 17-2-8
 * Time: 上午11:04
 */

namespace app\backend\controller;

use app\backend\controller\traits\IncomeNoticeList;
use app\backend\model\BatchPayment;
use app\backend\model\Building;
use app\backend\model\Lease;
use Business\CMBC\CmbcSDK;
use think\Controller;
use app\backend\model\BillManager;
use app\backend\model\Member;
use app\backend\model\JournalOnline;
use app\backend\model\JournalPlatform;
use Business\Fuyou\Fuyou;
use Business\TongLian\AppUtil;

class AlipayNotifyController extends Controller
{
    use IncomeNoticeList;
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 富友支付B2C异步回调通知
     * @date  2017-06-12
     * @author wuhao
     * @return string
     */
    public function fuYouB2CNotify()
    {
        $mchntId = I('mchnt_cd');
        $orderId = I('order_id');
        $orderTime = I('order_date');
        $amount = I('order_amt');
        $orderStatus = I('order_st');
        $payStatus = I('order_pay_code');
        $error = I('order_pay_error');
        $resv1 = I('resv1');
        $fuYouOrderId = I('fy_ssn');
        $md5 =I('md5');

//        $orderStatus = '11';
//        $md5 = '96f20cb8e3e5f60ced9fdac5c7a4be82';
//        $fuYouOrderId = '000044529155';
//        $payStatus = '0000';
//        $mchntId = '0001000F0040992';
//        $amount = 2;
//        $error = '';
//        $resv1 = '备注信息';
//        $orderTime = '20170426';
//        $orderId = '14931893955020171212201801111';

        zft_log('fuyou/notify', print_r($_REQUEST, true));

        $fuYou = new Fuyou();
        $mchntKey = $fuYou->config[ENV]['mchntKey'];

        $paramArr = [
            'mchnt_cd' => $mchntId,
            'order_id' => $orderId,
            'order_date' => $orderTime,
            'order_amt' => $amount,
            'order_st' => $orderStatus,
            'order_pay_code' => $payStatus,
            'order_pay_error' => $error,
            'resv1' => $resv1,
            'fy_ssn' => $fuYouOrderId,
            'mchntKey' => $mchntKey,
        ];

        $checkMd5 = $fuYou->FuYouStr($paramArr);

        if ($md5 != $checkMd5 && $payStatus !== '0000' && $orderStatus !== '11') {
            exit('wrong');
        }
        $orderType        = substr($orderId, -1);//账单类型
        $type=1;//富友B2C

        $journalOnlineModel = new JournalOnline();

        $journalOnlineModel->setValue('_serialNum', $orderId);
        //获取这条记录的信息
        $orderInfo  = $journalOnlineModel->getRecordBySerial();
        if ($orderInfo['status'] == 1) {//已经处理过的账单不再处理
            exit('success');
        }
        //账单id数组
        $orderIdArr = json_decode($orderInfo['order_id'], true);
        $beginCycle = $orderInfo['begin_cycle'];
        $endCycle   = $orderInfo['end_cycle'];
        $orderNum   = $orderInfo['ordernum'];

        //更新在线支付记录
        $journalOnlineModel->setValue('_tradeNumber', $fuYouOrderId);
        $journalOnlineModel->setValue('_time', time());
        $journalOnlineModel->setValue('_status', 1);
        $journalOnlineModel->setValue('_beginCycle', $beginCycle);
        $journalOnlineModel->setValue('_endCycle', $endCycle);
        $journalOnlineModel->setValue('_type', $orderType);
        $addRes = $journalOnlineModel->updateRecord();

        if (!$addRes) {
            zft_log('fuyou/updateRecordFail', print_r($journalOnlineModel->getLastSql(), true));
        }
        $memberModel = new Member();
        $billModel   = new BillManager();
        //更新租赁账单
        if ($orderType == 1) {
            //更新订单状态获取交易详情数组
            $billDetail = $this->_handleLeaseOrder($orderIdArr, $orderNum, $beginCycle, $endCycle, $type, $fuYouOrderId, $billModel, $memberModel);

            //发送对应短信
            if ($billDetail) {
                $this->_sendSms($billDetail, $orderNum, $beginCycle, $endCycle);
                echo 'success';
            } else {
                echo 'fail';
            }

        } else {
            $map   = ['ordernum' => $orderNum, 'status' => 0, 'begin_cycle' => $beginCycle,'end_cycle' => $endCycle, 'is_delete' => 0];
            $field = 'should_rent-share_money as money, owner_id,tenant_id, id';
            //获取该订单应付总额
            $trustBillInfo = $billModel->getTrustBill($map, $field);
            if ($trustBillInfo) {
                $trustBillInfo = $trustBillInfo[0];

                //付款方新增减钱记录
                $lastMoney = $memberModel->getMemberMoney($trustBillInfo['tenant_id']);
                $journalPlatformModel = new JournalPlatform();
                $journalPlatformModel->setAllProperty($trustBillInfo['tenant_id'], $amount, $lastMoney,$trustBillInfo['id'], $orderNum, $trustBillInfo['owner_id'], 1, time(), 0, 5, '富友缴费', $orderId, 22, $beginCycle, $endCycle);
                $journalRes = $journalPlatformModel->addRecord();
                if (!$journalRes) {
                    zft_log('addJournalFail', print_r($journalPlatformModel->getLastSql(), true));
                } else {
                    //给收款方加钱
                    $res = $memberModel->modifyMemberMoney($trustBillInfo['owner_id'], $trustBillInfo['tenant_id'], 0, $amount, 0, 5,$trustBillInfo['id'], $orderNum, 22, '富友缴费', $orderId, $beginCycle, $endCycle);
                    if ($res['code'] != 200) {
                        zft_log('modifyMemberMoney', print_r($res['msg'], true));
                    }
                    //设置订单为已支付状态
                    $updateRes = $billModel->updateTrustBill(['id' => $trustBillInfo['id']], ['status' => 1]);
                    if ($updateRes) {
                        zft_log('alipay_trust/ok', "$orderId|更新账单状态成功");
                    } else {
                        zft_log('set_trustbill_fail', print_r($billModel->getLastSql(), true));
                        zft_log('alipay_trust/error', "$orderId|更新账单状态失败");
                    }
                }
                echo 'success';
            } else {
                zft_log('alipay_trust/error', "4:$orderId|缴费账单不存在");
                echo 'fail';
            }
        }
    }

    /**
     * 富友B2B支付异步回调通知
     * @date  2017-06-12
     * @author wuhao
     * @return string
     */
    public function fuYouB2BNotify()
    {
        $mchntId = I('mchnt_cd');
        $orderId = I('order_id');
        $orderTime = I('order_date');
        $amount = I('order_amt');
        $orderStatus = I('order_st');
        $payStatus = I('order_pay_code');
        $error = I('order_pay_error');
        $resv1 = I('resv1');
        $fuYouOrderId = I('fy_ssn');
        $md5 =I('md5');

//        $orderStatus = '11';
//        $md5 = '96f20cb8e3e5f60ced9fdac5c7a4be82';
//        $fuYouOrderId = '000044529155';
//        $payStatus = '0000';
//        $mchntId = '0001000F0040992';
//        $amount = 2;
//        $error = '';
//        $resv1 = '备注信息';
//        $orderTime = '20170426';
//        $orderId = '14931893955020171212201801111';

        zft_log('fuyou/notify', print_r($_REQUEST, true));

        $fuYou = new Fuyou();
        $mchntKey = $fuYou->config[ENV]['mchntKey'];

        $paramArr = [
            'mchnt_cd' => $mchntId,
            'order_id' => $orderId,
            'order_date' => $orderTime,
            'order_amt' => $amount,
            'order_st' => $orderStatus,
            'order_pay_code' => $payStatus,
            'order_pay_error' => $error,
            'resv1' => $resv1,
            'fy_ssn' => $fuYouOrderId,
            'mchntKey' => $mchntKey,
        ];

        $checkMd5 = $fuYou->FuYouStr($paramArr);

        if ($md5 != $checkMd5 && $payStatus !== '0000' && $orderStatus !== '11') {
            exit('wrong');
        }
        $type        = substr($orderId, -1);//账单类型

        $journalOnlineModel = new JournalOnline();

        $journalOnlineModel->setValue('_serialNum', $orderId);
        //获取这条记录的信息
        $orderInfo  = $journalOnlineModel->getRecordBySerial();
        if ($orderInfo['status'] == 1) {//已经处理过的账单不再处理
            exit('success');
        }
        //账单id数组
        $orderIdArr = json_decode($orderInfo['order_id'], true);
        $beginCycle = $orderInfo['begin_cycle'];
        $endCycle   = $orderInfo['end_cycle'];
        $orderNum   = $orderInfo['ordernum'];

        //更新在线支付记录
        $journalOnlineModel->setValue('_tradeNumber', $fuYouOrderId);
        $journalOnlineModel->setValue('_time', time());
        $journalOnlineModel->setValue('_status', 1);
        $journalOnlineModel->setValue('_beginCycle', $beginCycle);
        $journalOnlineModel->setValue('_endCycle', $endCycle);
        $journalOnlineModel->setValue('_type', $type);
        $addRes = $journalOnlineModel->updateRecord();

        if (!$addRes) {
            zft_log('fuyou/updateRecordFail', print_r($journalOnlineModel->getLastSql(), true));
        }


        $memberModel = new Member();
        $billModel   = new BillManager();
        //更新租赁账单
        if ($type == 1) {
            //更新订单状态获取交易详情数组
            $billDetail = $this->_handleLeaseOrder($orderIdArr, $orderNum, $beginCycle, $endCycle, $type, $fuYouOrderId, $billModel, $memberModel);

            //发送对应短信
            if ($billDetail) {
                $this->_sendSms($billDetail, $orderNum, $beginCycle, $endCycle);
                echo 'success';
            } else {
                echo 'fail';
            }
        } else {
            $map   = ['ordernum' => $orderNum, 'status' => 0, 'begin_cycle' => $beginCycle,'end_cycle' => $endCycle, 'is_delete' => 0];
            $field = 'should_rent-share_money as money, owner_id,tenant_id, id';
            //获取该订单应付总额
            $trustBillInfo = $billModel->getTrustBill($map, $field);
            if ($trustBillInfo) {
                $trustBillInfo = $trustBillInfo[0];

                //付款方新增减钱记录
                $lastMoney = $memberModel->getMemberMoney($trustBillInfo['tenant_id']);
                $journalPlatformModel = new JournalPlatform();
                $journalPlatformModel->setAllProperty($trustBillInfo['tenant_id'], $amount, $lastMoney,$trustBillInfo['id'], $orderNum, $trustBillInfo['owner_id'], 1, time(), 0, 6, '富友缴费', $orderId, 22, $beginCycle, $endCycle);
                $journalRes = $journalPlatformModel->addRecord();
                if (!$journalRes) {
                    zft_log('addJournalFail', print_r($journalPlatformModel->getLastSql(), true));
                } else {
                    //给收款方加钱
                    $res = $memberModel->modifyMemberMoney($trustBillInfo['owner_id'], $trustBillInfo['tenant_id'], 0, $amount, 0, 6,$trustBillInfo['id'], $orderNum, 22, '富友缴费', $orderId, $beginCycle, $endCycle);
                    if ($res['code'] != 200) {
                        zft_log('modifyMemberMoney', print_r($res['msg'], true));
                    }
                    //设置订单为已支付状态
                    $updateRes = $billModel->updateTrustBill(['id' => $trustBillInfo['id']], ['status' => 1]);
                    if ($updateRes) {
                        zft_log('alipay_trust/ok', "$orderId|更新账单状态成功");
                    } else {
                        zft_log('set_trustbill_fail', print_r($billModel->getLastSql(), true));
                        zft_log('alipay_trust/error', "$orderId|更新账单状态失败");
                    }
                }
                echo 'success';
            } else {
                zft_log('alipay_trust/error', "4:$orderId|缴费账单不存在");
                echo 'fail';
            }
        }
    }

    /**
     * 通联回调函数
     * @date   2017-02-15
     * @author Lanlc
     *
     * @param
     *
     * @return
     */
    public function notify()
    {
        header("Content-type: text/html; charset=gb2312");
        $params = array();
        foreach ($_POST as $key=>$val) { //动态遍历获取所有收到的参数,此步非常关键,因为收银宝以后可能会加字段,动态获取可以兼容由于收银宝加字段而引起的签名异常
            $params[$key] = $val;
        }
        /*
        $params = [
            'acct' => '2088012946737623',
            'appid' => '00009003',
            'chnltrxid' => '2017032321001004620251417397',
            'cusid' => '288391048166298',
            'cusorderid' => '123456789012345820170222201702231',
            'outtrxid' => '123456789012345820170222201702231',
            'paytime' => '20170323150436',
            'sign' => 'C7F8FC90AB5440814538ED2C5EED9262',
            'termauthno' => '17126326216',
            'termrefnum' => '2017032321001004620251369715',
            'termtraceno' => 0,
            'trxamt' => 1,
            'trxcode' => 'VSP511',
            'trxdate' => '20170323',
            'trxid' => '348527716',
            'trxreserved' => 'test',
            'trxstatus' => '0000',
        ];
        */
        if (count($params) < 1) {   //如果参数为空,则不进行处理
            zft_log('tongLianPay', print_r('empty', true));
            echo "error";
            exit();
        }

        switch ($params['trxstatus']) {
            case '3044': $msg = '交易超时';break;
            case '3008': $msg = '余额不足';break;
            case '3999': $msg = '交易失败';break;
            default:break;
        }

        zft_log('tongLian/notify', print_r($params, true));
        $appUtil = new AppUtil();
        $config = parse_ini_file(HTML_DIR . '/extend/Business/TongLian/tongLianConfig.ini', true);
        if ($appUtil->ValidSign($params, $config[ENV]['appKey']) && $params['trxstatus'] == '0000') { //验签成功
            //此处进行业务逻辑处理
            $serialNum  = $params['cusorderid'];
            $orderNum   = substr($params['cusorderid'], 0, 12);
            $beginCycle = substr($params['cusorderid'], 12, 8);
            $endCycle   = substr($params['cusorderid'], 20, 8);
            $orderType  = substr($params['cusorderid'], 28, 1);
            $orderId    = $params['chnltrxid'];
            $payType    = $params['trxcode'];
            if ($payType == 'VSP511') {//支付宝
                $type = 2;
            } else {//微信
                $type = 3;
            }

            $billModel  = new BillManager();
            $memberModel = new Member();
            $money = $params['trxamt'];
            //更新在线支付记录
            $journalOnlineModel = new JournalOnline();
            $journalOnlineModel->setValue('_tradeNumber', $orderId);
            $journalOnlineModel->setValue('_time', time());
            $journalOnlineModel->setValue('_status', 1);

            $journalOnlineModel->setValue('_serialNum', $serialNum);
            $journalOnlineModel->setValue('_beginCycle', $beginCycle);
            $journalOnlineModel->setValue('_endCycle', $endCycle);
            $journalOnlineModel->setValue('_type', $orderType);

            //获取这条账单的基本信息
            $orderInfo = $journalOnlineModel->getRecordBySerial();
            if ($orderInfo['status'] == 1) {//已经处理过的账单不再处理
                exit('success');
            }

            //未处理过的
            $addRes = $journalOnlineModel->updateRecord();
            if (!$addRes) {
                zft_log('tongLian/updateRecordFail', print_r($journalOnlineModel->getLastSql(), true));
            }

            //账单id数组
            $orderIdArr = json_decode($orderInfo['order_id'], true);
            //echo $journalOnlineModel->getLastSql();die;

            $batchPayModel = new BatchPayment();
            if ($orderType == 1) {
                //更新订单状态获取交易详情数组
                $billDetail = $this->_handleLeaseOrder($orderIdArr, $orderNum, $beginCycle, $endCycle, $type, $orderId, $billModel, $memberModel);

                //发送对应短信
                if ($billDetail) {
                    $this->_sendSms($billDetail, $orderNum, $beginCycle, $endCycle);
                }
                //新增批量代付（清分）记录
                $res = $batchPayModel->addRecord($billDetail);
                if (!$res) {
                    zft_log('add_batch_pay', $batchPayModel->getLastSql());
                }
                if ($billDetail) {
                    echo 'success';
                } else {
                    echo 'fail';
                }
            } else {
                $map   = ['ordernum' => $orderNum, 'status' => 0, 'begin_cycle' => $beginCycle,'end_cycle' => $endCycle, 'is_delete' => 0];
                $field = 'should_rent-share_money as money, owner_id,tenant_id, id';
                //获取该订单应付总额
                $trustBillInfo = $billModel->getTrustBill($map, $field);

                if ($trustBillInfo) {
                    $trustBillInfo = $trustBillInfo[0];

                    //付款方新增减钱记录
                    $lastMoney = $memberModel->getMemberMoney($trustBillInfo['tenant_id']);
                    $journalPlatformModel = new JournalPlatform();
                    $journalPlatformModel->setAllProperty($trustBillInfo['tenant_id'], $money, $lastMoney,$trustBillInfo['id'], $orderNum, $trustBillInfo['owner_id'], 1, time(), 0, $type, '通联缴费', $orderId, 22, $beginCycle, $endCycle);
                    $journalRes = $journalPlatformModel->addRecord();
                    if (!$journalRes) {
                        zft_log('addJournalFail', print_r($journalPlatformModel->getLastSql(), true));
                    } else {
                        //给收款方加钱
                        $res = $memberModel->modifyMemberMoney($trustBillInfo['owner_id'], $trustBillInfo['tenant_id'], 0, $money, 0, $type, $trustBillInfo['id'], $orderNum, 22, '通联缴费', $orderId, $beginCycle, $endCycle);
                        if ($res['code'] != 200) {
                            zft_log('modifyMemberMoney', print_r($res['msg'], true));
                        }
                        //设置订单为已支付状态
                        $updateRes = $billModel->updateTrustBill(['id' => $trustBillInfo['id']], ['status' => 1]);
                        if ($updateRes) {
                            zft_log('alipay_trust/ok', "$orderId|更新账单状态成功");
                        } else {
                            zft_log('set_trustbill_fail', print_r($billModel->getLastSql(), true));
                            zft_log('alipay_trust/error', "$orderId|更新账单状态失败");
                        }
                    }
                    echo 'success';
                } else {
                    zft_log('alipay_trust/error', "4:$orderId|缴费账单不存在");
                    echo 'fail';
                }
            }
            echo "success";
        } else {
            zft_log('tongLian/notify', $params['cusorderid'].'验签失败');
            echo "error";
        }
    }

    /**
     * 民生回调函数
     * @date   2017-06-13
     * @author wuhao
     */
//    public function cmbcNotify()
//    {
//        $raw = file_get_contents('php://input');
//        $cmbcModel = new CmbcSDK();
//        $result = $cmbcModel->PayNotify($raw);
//        if ($result['return_code']!=='SUCCESS') {
//            zft_log('cmbc/order', "fail:" . json_encode($result, JSON_UNESCAPED_UNICODE));
//            exit('FAIL');
//        }
//        if ($result['data']['tradeStatus']!=='S') {
//            exit('FAIL');
//        }
//        $payType = substr($result['data']['orderNo'], 0, 1);
//        if ($payType == 'A') {
//            $type  = 5;
//        } else {
//            $type  = 6;
//        }
//
//        $serialNum  = $result['data']['orderNo'];
//        $orderNum   = substr($result['data']['orderNo'], 1, 12);
//        $beginCycle = substr($result['data']['orderNo'], 13, 8);
//        $endCycle   = substr($result['data']['orderNo'], 21, 8);
//        $orderType  = substr($result['data']['orderNo'], 29, 1);
//        $tradeNo    = $result['data']['voucherNo'].'|'.$result['data']['bankTradeNo'];
//        $money      = $result['data']['amount'];
//        zft_log('cmbc/notifyInfo', date('Y-m-d H:i:s').'#'.json_encode($result, JSON_UNESCAPED_UNICODE));
//
//        $billModel  = new BillManager();
//        $memberModel = new Member();
//        //更新在线支付记录
//        $journalOnlineModel = new JournalOnline();
//        $journalOnlineModel->setValue('_tradeNumber', $tradeNo);
//        $journalOnlineModel->setValue('_time', time());
//        $journalOnlineModel->setValue('_status', 1);
//
//        $journalOnlineModel->setValue('_serialNum', $serialNum);
//        $journalOnlineModel->setValue('_beginCycle', $beginCycle);
//        $journalOnlineModel->setValue('_endCycle', $endCycle);
//        $journalOnlineModel->setValue('_type', $orderType);
//
//        $addRes = $journalOnlineModel->updateRecord();
//        if (!$addRes) {
//            zft_log('cmbc/updateRecordFail', print_r($journalOnlineModel->getLastSql(), true));
//        }
//        //获取这条账单的基本信息
//        $orderInfo = $journalOnlineModel->getRecordBySerial();
//        //账单id数组
//        $orderIdArr = json_decode($orderInfo['order_id'], true);
//        //echo $journalOnlineModel->getLastSql();die;
//
//
//        if ($orderType == 1) {
//            $res = $this->_handleLeaseOrder($orderIdArr, $orderNum, $beginCycle, $endCycle, $type, $tradeNo, $billModel, $memberModel);
//            if ($res === 1) {
//                echo 'SUCCESS';
//            } else {
//                echo 'FAIL';
//            }
//        } else {
//            $map   = ['ordernum' => $orderNum, 'status' => 0, 'begin_cycle' => $beginCycle,'end_cycle' => $endCycle, 'is_delete' => 0];
//            $field = 'should_rent-share_money as money, owner_id,tenant_id, id';
//            //获取该订单应付总额
//            $trustBillInfo = $billModel->getTrustBill($map, $field);
//
//            if ($trustBillInfo) {
//                $trustBillInfo = $trustBillInfo[0];
//
//                //付款方新增减钱记录
//                $lastMoney = $memberModel->getMemberMoney($trustBillInfo['tenant_id']);
//                $journalPlatformModel = new JournalPlatform();
//                $journalPlatformModel->setAllProperty($trustBillInfo['tenant_id'], $money, $lastMoney,$trustBillInfo['id'], $orderNum, $trustBillInfo['owner_id'], 1, time(), 0, $type, '民生缴费', $tradeNo, 22, $beginCycle, $endCycle);
//                $journalRes = $journalPlatformModel->addRecord();
//                if (!$journalRes) {
//                    zft_log('addJournalFail', print_r($journalPlatformModel->getLastSql(), true));
//                } else {
//                    //给收款方加钱
//                    $res = $memberModel->modifyMemberMoney($trustBillInfo['owner_id'], $trustBillInfo['tenant_id'], 0, $money, 0, $type, $trustBillInfo['id'], $orderNum, 22, '民生缴费', $tradeNo, $beginCycle, $endCycle);
//                    if ($res['code'] != 200) {
//                        zft_log('modifyMemberMoney', print_r($res['msg'], true));
//                    }
//                    //设置订单为已支付状态
//                    $updateRes = $billModel->updateTrustBill(['id' => $trustBillInfo['id']], ['status' => 1]);
//                    if ($updateRes) {
//                        zft_log('alipay_trust/ok', "$tradeNo|更新账单状态成功");
//                    } else {
//                        zft_log('set_trustbill_fail', print_r($billModel->getLastSql(), true));
//                        zft_log('alipay_trust/error', "$tradeNo|更新账单状态失败");
//                    }
//                }
//                echo 'SUCCESS';
//            } else {
//                zft_log('alipay_trust/error', "4:$tradeNo|缴费账单不存在");
//                echo 'FAIL';
//            }
//        }
//    }

    /**
     * 处理订单并发送短信
     * @date   2017-06-13
     * @author wuhao
     */
    private function _handleLeaseOrder($orderIdArr, $orderNum, $beginCycle, $endCycle, $payType, $tradeNo, BillManager $billModel, Member $memberModel)
    {
        $map   = ['id' => ['in', $orderIdArr], 'status' => 0, 'begin_cycle' => $beginCycle, 'end_cycle' => $endCycle, 'is_delete' => 0];
        $field = 'id, money, owner_id, tenant_id, type_money, building_id';
        //获取账单数据
        $orderData = $billModel->getPayBillByOrderNum($orderNum, $field, $map);

        if ($orderData) {
            //收款消息队列
            $incomeNotice = [];

            //租户余额
            $tenantId  = $orderData[0]['tenant_id'];
            $lastMoney = $memberModel->getMemberMoney($tenantId);

            $journalPlatformModel = new JournalPlatform();

            $payName = $journalPayType = '';
            if ($payType == 1 || $payType == 4) {
                $payName='富友支付';
                $journalPayType = $payType == 1 ? 5 : 6;
            } elseif ($payType == 2 || $payType == 3) {
                $payName='通联支付';
                $journalPayType = $payType == 2 ? 2 : 3;
            }
            foreach ($orderData as $key => $val) {
                //付款方减钱记录
                $journalPlatformModel->setAllProperty($val['tenant_id'], $val['money'], $lastMoney, $val['id'], $orderNum, $val['owner_id'], 1, time(), 0, $journalPayType, $payName, $tradeNo, $val['type_money'], $beginCycle, $endCycle);
                $journalRes = $journalPlatformModel->addRecord();
                if (!$journalRes) {
                    zft_log('addJournalFail', print_r($journalPlatformModel->getLastSql(), true));
                } else {
                    //收款方加钱
                    $res = $memberModel->modifyMemberMoney($val['owner_id'], $val['tenant_id'], 0, $val['money'], 0, $journalPayType, $val['id'], $orderNum, $val['type_money'], $payName, $tradeNo, $beginCycle, $endCycle);
                    if ($res['code'] != 200) {
                        zft_log('modifyMemberMoney', print_r($res['msg'], true));
                    }
                    //设置订单为已支付状态
                    $updateRes = $billModel->updatePayBill(['id' => $val['id']], ['status' => 1, 'pay_time'=>date('Y-m-d H:i:s')]);
                    if ($updateRes) {

                        zft_log('alipay_lease/ok', "$orderNum|更新账单状态成功");
                    } else {
                        zft_log('set_paybill_fail', print_r($billModel->getLastSql(), true));
                        zft_log('alipay_lease/error', "$orderNum|更新账单状态失败");
                    }
                }
                //保存至数组
                $serviceMoney = ceil($val['money'] * (TONGLIAN_SERVICE));
                $actualMoney = $val['money'] - $serviceMoney;

                $tmp = [//基本的付款信息
                    'mid'         => $val['owner_id'],//收款人
                    'payer_id'    => $tenantId,//付款人
                    'begin_cycle' => $beginCycle,
                    'end_cycle'   => $endCycle,
                    'money'       => $val['money'],
                    'ordernum'    => $orderNum,
                    'type_money'  => $val['type_money'],
                    'building_id' => $val['building_id'],
                    'actual_money' => $actualMoney,
                    'service_money' => $serviceMoney,
                ];
                array_push($incomeNotice, $tmp);
                unset($actualMoney, $serviceMoney);
            }
            return $incomeNotice;
        } else {
            zft_log('alipay_lease/error', "4:$orderNum|缴费账单不存在");
            return [];
        }
    }

    /**
     * 生成短信队列，并发送
     * @param $billDetail
     * @param $orderNum
     * @param $beginCycle
     * @param $endCycle
     */
    private function _sendSms($billDetail, $orderNum,  $beginCycle, $endCycle)
    {
        if (!$billDetail) {return false;}
        //新增至消息队列
        if ($this->addIncomeNotice($billDetail, $orderNum, $billDetail[0]['payer_id'])) {
            //获取未发送的短信列表
            $smsList = $this->getIncomeNotice($orderNum, $beginCycle, $endCycle, 1, 0, 0);
            if ($smsList) {//发送短信
                //获取对应的房号
                $leaseModel = new Lease();
                $roomArr    = $leaseModel->getLeaseExtByOrderNum($orderNum, 'room');
                $roomArr    = array_column($roomArr, 'room');
                //获取楼宇名称
                $buildingId = $leaseModel->getLeaseByOrderNum($orderNum, 'building_id');
                $buildModel = new Building();
                $buildName = $buildModel->getBuildingName($buildingId['building_id']);
                $idArr      = $this->sendSms($smsList, $roomArr, $buildName['name']);
                if ($idArr) {//更新这些id的状态
                    $this->updateSendStatus($idArr, 1, time());
                }
            }
        }
    }
}
